package leetcode;

/**
 * <p></p>
 *
 * @author bin.zhang
 * <p/>
 * Revision History:
 * 2020/09/19, 初始化版本
 * @version 1.0
 **/
public class Convert {

	public String convert(String s, int numRows) {
		if(s == null || s.length() == 0) return "";
		if(s.length() == 1 || numRows == 0 || numRows == 1 || s.length() <= numRows) return s;
		StringBuilder sb = new StringBuilder();
		int strLength = s.length();
		StringBuilder[] matrix = new StringBuilder[numRows];
		int x = 0;
		boolean goingDown = false;
		char[] strArray = s.toCharArray();
		for (int i = 0; i < strLength; i ++){
			if(matrix[x] == null){
				matrix[x] = new StringBuilder();
			}
			matrix[x].append(strArray[i]);
			if (x == 0 || x == numRows - 1) goingDown = !goingDown;
			x += goingDown ? 1 : -1;
		}
		for (int i = 0; i < numRows; i++) sb.append(matrix[i]);
		return sb.toString();
	}

	public static void main(String[] args){
		System.out.println(new Convert().convert("LEETCODEISHIRING", 4));
	}

}
